package com.knowledgeqa.system.service.impl;

import com.knowledgeqa.system.entity.Answer;
import com.knowledgeqa.system.mapper.AnswerMapper;
import com.knowledgeqa.system.service.AnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 回答服务实现类
 */
@Service
public class AnswerServiceImpl implements AnswerService {

    @Autowired
    private AnswerMapper answerMapper;

    @Override
    public boolean addAnswer(Answer answer) {
        // 设置默认值
        answer.setIsAccepted(0); // 未被采纳
        answer.setCreateTime(new Date());
        answer.setUpdateTime(new Date());
        
        return answerMapper.insert(answer) > 0;
    }

    @Override
    public Answer findById(Long id) {
        return answerMapper.selectById(id);
    }

    @Override
    public List<Answer> findByQuestionId(Long questionId) {
        return answerMapper.selectByQuestionId(questionId);
    }

    @Override
    public List<Answer> findByUserId(Long userId) {
        return answerMapper.selectByUserId(userId);
    }

    @Override
    public boolean update(Answer answer) {
        answer.setUpdateTime(new Date());
        return answerMapper.update(answer) > 0;
    }

    @Override
    public boolean deleteById(Long id) {
        return answerMapper.deleteById(id) > 0;
    }

    @Override
    public Answer findAcceptedAnswerByQuestionId(Long questionId) {
        return answerMapper.selectAcceptedAnswerByQuestionId(questionId);
    }
}